package com.hb.trade.server.center.au.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.center.au.mapper.RoleResourceMapper;
import com.hb.core.pojo.center.au.entity.RoleResource;
import com.hb.core.pojo.center.sys.dto.MenuResourceDTO;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.trade.server.center.au.service.RoleResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.trade.server.center.sys.service.MenuResourceService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色权限中间表 服务实现类
 * </p>
 *
 * @author zhaojk
 * @since 2021-11-29
 */
@Service
public class RoleResourceServiceImpl extends ServiceImpl<RoleResourceMapper, RoleResource> implements RoleResourceService {

    protected final MenuResourceService menuResourceService;

    public RoleResourceServiceImpl(MenuResourceService menuResourceService) {
        this.menuResourceService = menuResourceService;
    }

    /**
     * 更新角色权限
     *
     * @param roleId        角色ID
     * @param menuResources 系统菜单集合
     * @return
     */
    @Override
    public boolean updatePermissions(Long roleId, List<MenuResourceDTO> menuResources) {
        List<Long> resIds = CollUtil.map(menuResources, MenuResourceDTO::getId, true);
        return updatePermissionsWithMenuIds(roleId, resIds);
    }

    /**
     * 更新角色权限
     *
     * @param roleId      角色ID
     * @param resourceIds 系统菜单ID集合
     * @return
     */
    @Override
    public boolean updatePermissionsWithMenuIds(Long roleId, List<Long> resourceIds) {
        if (roleId == null || CollUtil.isEmpty(resourceIds)) {
            return false;
        }

        // 查询所有active_menu为0的角色, activeMenu为0的在前端表示为路由信息, 所有用户都拥有路由信息的权限
        // 但activeMenu为0时不在前端左侧菜单栏展示
        MenuResourceDTO resourceDTO = new MenuResourceDTO();
        resourceDTO.setActiveMenu(0);
        List<MenuResourceDTO> resourceDTOS = menuResourceService.listAllMenus(resourceDTO);
        List<Long> activeMenu0 = CollUtil.map(resourceDTOS, MPCommonEntity::getId, true);
        resourceIds.addAll(activeMenu0);

        // 删除原有权限
        QueryWrapper<RoleResource> wrapper = RoleResource.wrapper();
        wrapper.eq("role_id", roleId);
        remove(wrapper);

        List<RoleResource> roleResources = CollUtil.list(false);

        for (Long resId : resourceIds) {
            RoleResource rr = new RoleResource();
            // 设置角色ID
            rr.setRoleId(roleId);
            // 设置资源ID
            rr.setResId(resId);

            rr.buildBaseData(1);
            roleResources.add(rr);
        }

        if (roleResources.size() == 0) {
            return true;
        }

        // 添加新的权限
        return saveBatch(roleResources);
    }

    /**
     * 删除角色菜单表中的关联数据
     *
     * @param roleId
     * @return
     */
    @Override
    public boolean cleanRolePermissions(Long roleId) {
        if (roleId == null) {
            return false;
        }
        QueryWrapper<RoleResource> wrapper = RoleResource.wrapper();
        wrapper.eq("role_id", roleId);
        return remove(wrapper);
    }
}
